package com.lanou.student.page;

import java.awt.print.Printable;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.lanou.student.bean.Student;
import com.lanou.student.bean.User;
import com.lanou.student.controller.StudentController;
import com.lanou.student.controller.UserController;
import com.lanou.student.util.PasswordUtil;

/**
 * /打印所有用户信息 //添加 //更新 //删除 通过用户名删除 //通过用户去查 //注销
 *
 * @author Lenovo
 */
public class Index {
    private Scanner sc = new Scanner(System.in);
    private StudentController studentController;


    public Index() {
        studentController = new StudentController();
    }

    public void index() {
        boolean flag = true;
        while (flag) {
            System.out.println("=========欢迎学生信息管理系统首页=========");
            // 打印出所有的学生信息
            print();
            System.out.println("\t(1)、添加\t(2)、更新");
            System.out.println("\t(3)、删除\t(4)、注销");
            System.out.println("\t\t请选择");
            switch (sc.nextInt()) {
                case 1:
                    Student s = getStudent();
                    if (studentController.add(s)) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                case 2:
                    Student stu = updateStudent();
                    if (studentController.update(stu)) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    // 更新
                    break;
                case 3:
                    System.out.println("请输入你需要删除的id");
                    if (studentController.delete(sc.nextInt())) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    break;
                case 4:
                    flag = false;
                    break;

                default:
                    System.out.println("你选择有误，请重新选择");
                    break;
            }
        }

    }

    private Student updateStudent() {

        //先通过id查询学生是否存在
        Student student = null;
        while (student == null) {
            System.out.println("请输入学生的id");
            student = studentController.getStudent(sc.nextInt());
            if (null != student) {
                System.out.println(student);
                break;
            }
            System.out.println("学生不存在");
        }
        boolean flag = true;
        while (flag) {
            System.out.println("(1)、学生姓名\t(2)、年龄");
            System.out.println("(3)、数学成绩\t(4)、英语成绩");
            System.out.println("(5)、计算机\t(6)、是否修改");
            switch (sc.nextInt()) {
                case 1:
                    System.out.println("请输入学生姓名");
                    student.setStuName(sc.next());
                    break;
                case 2:
                    System.out.println("请输入学生年龄");
                    student.setStuAge(sc.nextInt());
                    break;
                case 3:
                    System.out.println("请输入数学成绩");
                    student.setMath(sc.nextDouble());
                    break;
                case 4:
                    System.out.println("请输入英语成绩");
                    student.setEng(sc.nextDouble());
                    break;
                case 5:
                    System.out.println("请输入计算机成绩");
                    student.setComputer(sc.nextDouble());
                    break;
                case 6:
					System.out.println("0:继续，1：修改");
                	if (sc.nextInt() == 1){
                		flag = false;
					}
                	break;

            }
        }

        return student;
    }

    private Student getStudent() {
        System.out.println("请输入学生姓名");
        String stuName = sc.next();
        System.out.println("请输入学生年龄");
        int stuAge = sc.nextInt();
        System.out.println("请输入学生数学成绩");
        double math = sc.nextDouble();
        System.out.println("请输入英语成绩");
        double eng = sc.nextDouble();
        System.out.println("请输入计算机成绩");
        double computer = sc.nextDouble();

        return new Student(studentController.getId(), stuName, stuAge, math, eng, computer);
    }

    private void print() {
        List<Student> students = studentController.getStudents();
        System.out.println("id\t姓名\t年龄\t数学\t英语\t计算机");


        for (Student student : students) {
            System.out.println(student);
        }

    }

}
